package com.ac.controller;

import java.util.List;

import org.bson.Document;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ac.aop.Auth;
import com.ac.model.AuthRole;
import com.ac.response.Res;
import com.ac.service.AuthRoleService;
import com.ac.service.AuthTokenService;
import com.ac.service.AuthUserService;
import com.ac.util.StrUtils;

import lombok.AllArgsConstructor;

@RestController
@RequestMapping("auth/role")
@AllArgsConstructor
public class AuthRoleController {

	private final AuthRoleService RoleService;
	private final AuthUserService UserService;
	private final AuthTokenService ts;

	@Auth
	@PostMapping(value = "list")
	public Res list(@RequestHeader("token") String token) {
		List<AuthRole> users = RoleService.list();
		Res res = Res.success(null, users, users.size());
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "roleList")
	public Res roleList(@RequestHeader("token") String token, @RequestBody Document req) {
		List<AuthRole> users = RoleService.roleList(UserService.info(ts.getUserId(token),true), req);
		Res res = Res.success(null, users, users.size());
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "save")
	public Res save(@RequestBody AuthRole api, @RequestHeader("token") String token) {
		if (StrUtils.isEmpty(api.getId()))
			RoleService.insert(api);
		else
			RoleService.update(api);
		Res res = Res.success("保存成功");
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "remove")
	public Res remove(@RequestBody AuthRole api, @RequestHeader("token") String token) {
		RoleService.remove(api);
		Res res = Res.success("删除成功");
		return ts.getToken(res, token);
	}

}
